﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MpSoft.Web.AdminCP.Controls.Configs
{
    public partial class SettingDefault : UserControl
    {
        #region Var

        private string tab = "banner";
        
        #endregion
        
        #region Method

        private UserControl LoadUserControl(string ControlPath)
        {
            try
            {
                return Page.LoadControl(ControlPath) as UserControl;
            }
            catch (HttpException ex)
            {
                Session["MessError"] = ex.Message.ToString();
                Session["MessError"] = Session["MessError"].ToString() + "</br></br>" +
                    ex.StackTrace.ToString();
                return Page.LoadControl("~/AdminCP/Controls/Error.ascx") as UserControl;
            }
        }

        private void BindControl()
        {
            UserControl control = null;
            switch (tab)
            {
                case "contact":
                    control = LoadUserControl("~/AdminCP/Controls/Configs/SetContact.ascx");
                    control.ID = "contact";
                    plControl.Controls.Add(control);
                    break;
                case "seo":
                    control = LoadUserControl("~/AdminCP/Controls/Configs/SetSeo.ascx");
                    control.ID = "seo";
                    plControl.Controls.Add(control);
                    break;
                case "email":
                    control = LoadUserControl("~/AdminCP/Controls/Configs/SetEmail.ascx");
                    control.ID = "email";
                    plControl.Controls.Add(control);
                    break;
                case "footer":
                    control = LoadUserControl("~/AdminCP/Controls/Configs/SetFooter.ascx");
                    control.ID = "footer";
                    plControl.Controls.Add(control);
                    break;
                default:
                    control = LoadUserControl("~/AdminCP/Controls/Configs/SetBanner.ascx");
                    control.ID = "banner";
                    plControl.Controls.Add(control);
                    break;
            }
        }

        protected string GetActive(string _tab)
        {
            if (_tab.Equals(tab))
                return "class='active'";
            if (string.IsNullOrEmpty(tab) && _tab.Equals("banner"))
                return "class='active'";
            return "";
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["tab"] != null)
                tab = Request.QueryString["tab"].ToString();
            BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (plControl.FindControl(this.tab )!= null)
            {
                switch (this.tab)
                {
                    case "email":
                        var email = plControl.FindControl("email") as SetEmail;
                        if (email != null)
                            email.Save();
                        break;
                    case "seo":
                        var seo = plControl.FindControl("seo") as SetSeo;
                        if (seo != null)
                            seo.Save();
                        break;
                    case "contact":
                        var contact = plControl.FindControl("contact") as SetContact;
                        if (contact != null)
                            contact.Save();
                        break;
                    case "footer":
                        var footer = plControl.FindControl("footer") as SetFooter;
                        if (footer != null)
                            footer.Save();
                        break;
                    default:
                        var banner = plControl.FindControl("banner") as SetBanner;
                        if (banner != null)
                            banner.Save();
                        break;
                }
            }
        }
        
        #endregion

        #region Properties

        public string Tab { get { return tab; } }

        #endregion
        
    }
}